<?php
App::uses('AppModel', 'Model');
/**
 * Channel Model
 *
 * @property User $User
 * @property Ad $Ad
 * @property Type $Type
 */
class Brand extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'name' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'url' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'description' => array(
			'notempty' => array(
				'rule' => array('notempty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'logo' => array(
			'image' => array(
				'rule' => array('extension',array('jpeg','jpg','png','gif')),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    /*
	public $belongsTo = array(
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
    */
/**
 * hasMany associations
 *
 * @var array
 */
	/*public $hasMany = array(
		'Ad' => array(
			'className' => 'Ad',
			'foreignKey' => 'channel_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);*/


/**
 * hasAndBelongsToMany associations
 *
 * @var array
 */
    /*
	public $hasAndBelongsToMany = array(
		'Type' => array(
			'className' => 'Type',
			'joinTable' => 'brands_types',
			'foreignKey' => 'brand_id',
			'associationForeignKey' => 'type_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
		)
	);
    */
	public $hasAndBelongsToMany = array(
		'User' => array(
			'className' => 'User',
			'joinTable' => 'brands_users',
			'foreignKey' => 'brand_id',
			'associationForeignKey' => 'user_id',
			'unique' => 'keepExisting',
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'finderQuery' => '',
		)
	);
	public $actsAs = array(
		'UploadPack.Upload' => array(
			'logo' => array(
				'styles' => array(
					'mini' => '90x90',
					'square' => '220x220',
				),
				'path' => ':webroot/upload/:model/:id/:style.:extension'
			),
		),
	);

	public function afterFind($results, $primary = false) {
		foreach ($results as $key => $val) {
			if (isset($val[$this->alias]['logo_file_name'])) {
				$ext = explode(".", strtolower($val[$this->alias]['logo_file_name']));
				$id = $val[$this->alias]['id'];
				$results[$key][$this->alias]['logo_mini'] = '/upload/brands/'.$id.'/mini.'.end($ext);
				$results[$key][$this->alias]['logo_square'] = '/upload/brands/'.$id.'/square.'.end($ext);
			}
		}
		return $results;
	}

	public function getMyBrands($id = null) {
	//public function getMyBrands() {
		//return $this->find('all', array('conditions' => array('Brand.user_id' => $id)));
		return $this->find('all', array(
                'joins'       	=> array(
                    array(
                        'table' => $this->hasAndBelongsToMany['User']['joinTable'],
                        'alias' => 'Middle',
                        'type'  => 'INNER',
                        'conditions'=> array("Middle.brand_id = Brand.id")
                    ),
                    array(
                        'table' => 'users', 
                        'alias' => 'User',
                        'type'  => 'INNER',
                        'conditions'=> array("User.id = Middle.user_id")
                    )
                ),
                'conditions'    => array('User.id' => $id),
            )
        );
	}

}
